package tpo.lmhconsumer.lmhconsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import dto.Dto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.Sort;
import pojo.Specs;
import tpo.lmhconsumer.lmhconsumer.service.SpecsService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品规格
 */
@RestController
public class SpecsController {
    @Autowired
    SpecsService specsService;

    @RequestMapping("getSpecs")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getSpecs(@RequestBody Map map){
        return specsService.getSpecs(map);
    }

    @RequestMapping("updateSpecs")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto updateSpecs(@RequestBody Map map){
        return specsService.updateSpecs(map);
    }

    @RequestMapping("addSpecs")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto addSpecs(@RequestBody Map map){
        return specsService.addSpecs(map);
    }

    @RequestMapping("delSpecs")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto delSpecs(@RequestBody Map map){
        return specsService.delSpecs(map);
    }

    @RequestMapping("getallSpecs")
    @HystrixCommand(fallbackMethod = "rd_map")
    public Dto getall(@RequestBody Map map) {
        List list=(List) specsService.getSpecs(map).getObj();
        list=this.getallSpecs(list);
        Dto dto=new Dto();
        dto.setObj(list);
        return dto;
    }

    private List<Specs> getallSpecs(List<Specs> list){
        if (list.size()>0){
            for (Specs sp : list){
                Map map=new HashMap();
                map.put("spId",sp.getId());
                List childList=(List) specsService.getSpecs(map).getObj();
                childList=getallSpecs(childList);
                sp.setChildList(childList);
            }
        }
        return list;
    }

    private Dto rd_map(Map map){
        Dto dto=new Dto();
        dto.setMsg("熔断");
        return dto;
    }
}
